// pages/login/login.js

import config from "../../utils/http/config";

const app = getApp()

const common_utils = require('../../utils/common_utils.js')
import Config from '../../utils/http/config.js'
import Api from '../../utils/http/api'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    from: '', //back返回上一页,其他返回首页
    to: '', //登录后指定跳转的页面地址
    channel: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("login ops=", options)

    if (options.from) { //从哪个页面进来
      this.data.from = options.from
    }

    if (options.to) {
      this.setData({
        to: options.to
      })
      if (common_utils.isTabPage(options.to)) {
        wx.switchTab({
          url: options.to
        })
      } else {
        wx.navigateTo({
          url: options.to,
        })
      }
    }

    //检查登录态是否过期
    wx.checkSession({
      success() {
        //session_key 未过期，并且在本生命周期一直有效
      },
      fail() {
        // session_key 已经失效，需要重新执行登录流程
        this.doWxLogin()
      }
    })


  },
  onBack() {
    console.log('22222222222222');
    if (this.data.from == 'my' || this.data.from == 'home' || this.data.from == 'shop') {
      wx.switchTab({
        url: '/pages/home/home',
      })
    } else {
      wx.navigateBack()
    }
  },
  //微信登录获取openId
  doWxLogin() {
    wx.login({
      success: res => {
        console.log('res.code', res.code);
        this.doGetOpenId(res.code)
      },
      fail: res => {
        wx.showToast({
          title: res.errMsg,
          icon: 'none',
          duration: 3000
        })
      }
    })
  },
  doGetOpenId(code) {
    // 发送 res.code 到后台换取 openId, sessionKey, unionId
    let params = {
      jscode: code
    }
    Api.getOpenId(params).then(res => {
      try {
        wx.setStorageSync('open_id', res.data.openid)
        wx.setStorageSync('session_key', res.data.session_key)

      } catch (e) {}
    })
  },
  getPhoneNumber(e) {
    console.log('e',e);
    console.log(e.detail.code) // 动态令牌
    console.log(e.detail.code) // 动态令牌
    console.log(e.detail.errMsg) // 回调信息（成功失败都会返回）
    console.log(e.detail.errno) // 错误码（失败时返回）
    if (e.detail.errMsg !== 'getPhoneNumber:ok') {
      //'拒绝授权'
      // wx.navigateBack()
      wx.switchTab({
        url: '/pages/my/my',
      })
    } else {
      //'同意授权'
      let params = {
        open_id: common_utils.getLocalStorageData('open_id'),
        code: e.detail.code
      }
      Api.getUserInfo(params).then(res => {
        let userInfo = res.data
        if (userInfo) {
          app.globalData.loginFlag = true
        } else {
          app.globalData.loginFlag = false
        }
        common_utils.saveUserinfo(userInfo)
        this.goNext()
      })

    }
  },
  goNext() {
    if (this.data.from == 'my') {
      wx.switchTab({
        url: '/pages/my/my',
      })
    } else if (this.data.from == 'home') {
      wx.switchTab({
        url: '/pages/home/home',
      })
    } else if (this.data.from == 'shop') {
      wx.switchTab({
        url: '/pages/shop/shop',
      })
    } else {
      wx.navigateBack()
    }
  },
})